package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Notice;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.util.JsonResult;
import cn.wolfcode.util.QueryObject;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Notice> pageResult = noticeService.query(qo);
        model.addAttribute("pageResult", pageResult);
        return "notice/list";
    }

    //跳转公告内容页面
    @RequestMapping("/concreteContent")
    public String concreteContent(Model model, Long noticeId){
        Notice notice = noticeService.get(noticeId);
        model.addAttribute("notice", notice);
        return "show/show";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"notice:delete","公告通知删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            noticeService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"notice:saveOrUpdate","公告通知新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Notice notice){
        if (notice.getId() != null) {
            noticeService.update(notice);
        }else {
            noticeService.save(notice);
        }
        return new JsonResult();
    }

    //保存员工阅读公告的情况
    @RequestMapping("/saveRead")
    @ResponseBody//不找视图
    public void saveRead(Long noticeId){
        noticeService.saveRead(noticeId);
    }


}
